package DDL;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import DuiXiang.TableModel;

public class GetDataBaseList {
	public Map<String,List<String>> getDataBaseList(){//列出所有数据库的所有表名
		File file = new File("D:\\database\\");
		Map<String,List<String>> dirName=new HashMap<String,List<String>>();
        if (file.isDirectory()) {  
        	
            File[] files = file.listFiles();  
            for (int i = 0; i < files.length; i++) {
            	List<String> filename=new ArrayList<String>();
                String name = files[i].getName();  
                if (files[i].isDirectory()) {  
                    String path = files[i].getPath();  
                    filename=listFileName(path);  
                }  
                dirName.put(name, filename);
            }  
        }  
        return dirName;
	}
	@SuppressWarnings("finally")
	public List<TableModel> getTableModel(String dataBaseName,String tableName){
		File tableFile = new File("D:\\database\\" + dataBaseName + "\\"
				+ tableName + ".dbf");
		if (!tableFile.exists()) {
			System.out.println("没有这张表");
			return null;
		}
		FileReader fileReader = null;
		BufferedReader bufferReader = null;
		List<TableModel> list=new ArrayList<TableModel>();
		try {
			fileReader = new FileReader(tableFile);
			bufferReader = new BufferedReader(fileReader);
			String tempString = null;
			try {
				String[] tempStringList;
				while ((tempString = bufferReader.readLine()) != null) {
					tempStringList = tempString.split(",");// 按逗号分隔到数组tempStringList
					TableModel tableModel=new TableModel();
					tableModel.setsFieldName(tempStringList[0].trim());
					tableModel.setsType(tempStringList[1].trim());
					tableModel.setiSize(Integer.parseInt(tempStringList[2].trim()));
					tableModel.setbKey(tempStringList[3].trim().charAt(0));
					tableModel.setbNullFlag(tempStringList[4].trim().charAt(0));
					tableModel.setbValidFlag(tempStringList[5].trim().charAt(0));
					list.add(tableModel);
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.out.println("读文件时异常");
				return null;
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("文件打开异常");
			return null;
		} finally {
			try {
				bufferReader.close();
				fileReader.close();
				return list;
			} catch (IOException e) {
				e.printStackTrace();
				System.out.println("关闭文件异常");
				return null;
			}
		}
	}
	public List<String> listFileName(String fileName){
		File file = new File(fileName);  
		String[] tempStringArray;
		List<String> listName=new ArrayList<String>();
        if (file.isDirectory()) {  
            File[] files = file.listFiles();  
            for (int i = 0; i < files.length; i++) {  
                String name = files[i].getName();  
                if (name.trim().toLowerCase().endsWith(".dbf")){
                	tempStringArray=name.split("\\.");
                	listName.add(tempStringArray[0]);
                }
            }  
        }  
        return listName;
	}
}
